import random
from time import localtime
f = open("output.txt", "w")

"""
start north rotate clockwise
0 = out, circle
1 = out, square
2 = in, circle
3 = in, square
0, 2 go together
1, 3 go together

puzzle:

0   1

2   3

"""

#VARIABLE DECLARATION
pieces = [
    [1,0,1,1],
    [0,2,3,2],
    [3,1,2,0],
    [1,3,0,3],
    ]


connections = []
solved = False

#FUNCTION DECLARATION
def rotate(x):
    #rotate
    for i in range(0,3):
        z = random.randint(0,3)
        count = 0
        while count < z:
            top = x[i][0]
            x[i].pop(0)
            x[i].append(top)
            count += 1
    return x

def flip(x):
    for i in range(0,3):
        z = random.randint(0,1)
        if z == 1:
            top = x[i][0]
            x[i][0] = x[i][2]
            x[i][2] = top
    return x

# this function shuffles the puzzle randomly
def rand_s(p):
    random.shuffle(p)
    p = rotate(p)
    p = flip(p)
    return p

# next there should be a function which checks to see if the value is valid
def is_solved(x):
    if  not are_connected(x[0][1],x[1][3]):
        return False
    if  not are_connected(x[1][2],x[3][0]):
        return False
    if  not are_connected(x[2][1],x[3][3]):
        return False
    if  not are_connected(x[2][0],x[0][2]):
        return False
    return True

def are_connected(x,y):
    if (x == 0 and y == 2 or x == 1 and y == 3):
        return True
    return False

#MAIN LOOP
count = 0
s = 0
#time = localtime()
while not(solved):
    pieces = rand_s(pieces)
    #print("not solved.")
    count += 1
    if is_solved(pieces):
        print("solved.")
        s += 1
        f.write("Solved " + s + "\n")
        f.write("data: " + str(repr(pieces)) + "\n")
        f.write("count: " + str(count)+"\n")
        #f.write("time (s): " + (str(localtime())-str(time)))
        f.write("\n\n")
        count = 0
